// 返回身份证信息 如 性别 sex、生日 birthDay
export function getCardInfo(id:any,daytype='') {
	// console.log(id, "id");
	if(!id) return;
	const obj:any = {};
	// 身份证号 获取性别
	const sexNum = id.substring(id.length - 2, id.length - 1);
	const isEven = (sexNum % 2) === 0;
	obj["sex"] = isEven ? "0" : "1"; // 姓别 1 -> 男 | 0 -> 女

	// 身份证号 获取生日
	const birthDay = id.substring(6, 14);
	const y = birthDay.substring(0, 4);
	const m = birthDay.substring(4, 6);
	const d = birthDay.substring(6, 8);
	if(daytype == '/'){
		obj["birthDay"] = y + "/" + m + "/" + d;
	}else{
		obj["birthDay"] = y + "-" + m + "-" + d;
	}

	// 身份证号 获取年龄
	const nowDate = new Date(); //获取当前时间
	const intYear = parseInt(y); //身份证的年份转为数字格式
	const month = nowDate.getMonth() + 1; //获取当前月份，getMonth()方法获取到的月份是从0开始的，所以这里需要加1
	const intMonth = parseInt(m); //身份证的月份转为数字格式
	const day = nowDate.getDate();
	let age:any = nowDate.getFullYear() - intYear - 1;
	age = parseInt(age);
	//判断身份证月份是否比当前月份小
	if (intMonth < month || (intMonth === month && id.substring(12, 14) <= day)) {
		age++;
	}
	obj["age"] = age;

	// 判断基本信息
	if (parseInt(y) <= 0 || parseInt(m) <= 0 || parseInt(d) <= 0 || parseInt(d) > 31 || age > 110) {
		alert("请检查身份证号");
		return false;
	}
	return obj;
}

export function getAge(birthday:any){
	// console.log(birthday);
	if(birthday == '' || !birthday) return '未知';
	//出生时间 毫秒
	const birthDayTime = new Date(birthday).getTime(); 
	//当前时间 毫秒
	const nowTime = new Date().getTime(); 
	//一年毫秒数(365 * 86400000 = 31536000000)
	return Math.ceil((nowTime-birthDayTime)/31536000000);
}

export function getphone(phone:any){
	if(phone == '' || !phone) return '未知';
	return phone.substr(0,3) + "****" + phone.substr(7)
}
